package com.one.dao;

import com.one.entity.User;
import com.one.utils.DbConn;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDao {
    //增删改查的工具
    private QueryRunner runner = new QueryRunner(DbConn.getDataSource());

    /**
     * user用户的增加
     */
    public boolean addUser(User user){
        Object[] params = {null,user.getUser_admin(),user.getUser_password(), user.getUser_name(),
                user.getUser_email(),user.getUser_sex(),user.getUser_address(),user.getUser_type()};
        String sql = "insert into user values(?,?,?,?,?,?,?,?)";
        try {
            return runner.update(sql,params)>0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * user用户的删除
     */
    public boolean delUser(int user_id){
        String sql = "delete from user where user_id=?";
        try {
            return runner.update(sql,user_id)>0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * user用户的修改
     */
    public boolean updUser(User user){
        Object[] params = {user.getUser_admin(),user.getUser_password(), user.getUser_name(),
                user.getUser_email(),user.getUser_sex(),user.getUser_address(),user.getUser_type(),user.getUser_id()};
        String sql = "update  user set user_admin=?,user_password=?," +
                "user_name=?,user_email=?,user_sex=?,user_address=?,user_type=? where user_id=?";
        try {
            return runner.update(sql,params)>0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 查询单个用户方法
     * @param username
     * @param password
     * @return
     */
    public User selUser(String username,String password){
        User user = null;
        String sql = "select * from user where user_admin=? and user_password = ?;";
        Object[] params = {
                username,
                password
        };
        try {
            user = runner.query(sql, new BeanHandler<>(User.class), params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }

    /**
     * 查询所有用户
     * @return
     */
    public List<User> selUsers(){
        List<User> users = new ArrayList<>();
        String sql = "select * from user";
        try {
            users = runner.query(sql, new BeanListHandler<>(User.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return users;
    }

    public User selUser(int user_id){
        User user = null;
        String sql = " select * from user where user_id = ?;";
        Object[] params = {
                user_id,
        };
        try {
            user = runner.query(sql, new BeanHandler<>(User.class), params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }


}
